package org.zfes.snowier.common.monitor.biz.service;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
import org.zfes.snowier.common.monitor.biz.dao.ThreadMapper;
import org.zfes.snowier.common.monitor.biz.model.ThreadEntity;
import org.zfes.snowier.common.monitor.util.JvmUtils;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


@Service
public class ThreadService {
    @Autowired
    private ThreadMapper threadMapper;

	@SuppressWarnings({ "unchecked", "rawtypes" })
	public List<ThreadEntity> findAllByInstanceKey(String instanceKey) {
    	Map<String, Object> paramMap=new HashMap<>();
    	paramMap.put("instanceKey", instanceKey);
        return threadMapper.selectList(new QueryWrapper(paramMap));
    }

    /**
     * 写入线程相关信息
     * @param name
     * @param date
     * @param jstatk
     */
    public void save(ThreadEntity entity) {
    	entity.setDate(JvmUtils.time());
    	threadMapper.insert(entity);
    }


 
    
    @Async
	public void save( String instanceKey,String threadJsonData) {
    	ThreadEntity threadz =	JSONObject.parseObject(threadJsonData, ThreadEntity.class);
    	threadz.setInstanceKey(instanceKey);
    	threadz.setGroupKey(StringUtils.substringBefore(instanceKey, "_"));
    	 
    	 save(threadz);
		
	}
    
    @Async
    public void clearAll() {
    	threadMapper.deleteAll();
    }
}
